package com.gongpingjialoan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.RecordBean;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/2/29.
 */
public class PriceChangeAdapter  extends BaseAdapter {


    private LayoutInflater mInflater;

    private Context context;

    public List<RecordBean.DataEntity.PriceChangeRemindsEntity> mCarSourceList;


    public PriceChangeAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<RecordBean.DataEntity.PriceChangeRemindsEntity> CarSourceList) {
        this.mCarSourceList = CarSourceList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCarSourceList == null) {
            return 0;
        }
        return mCarSourceList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCarSourceList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CarSourceViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.price_change_item, null);
            holder = new CarSourceViewHolder();
            holder.brandname = (TextView) convertView.findViewById(R.id.brand_name);
            holder.review = (TextView) convertView.findViewById(R.id.review);
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.renzhengImageView = (ImageView) convertView.findViewById(R.id.renzheng);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.tvCarPrice = (TextView) convertView.findViewById(R.id.price);
            holder.tvDomain = (TextView) convertView.findViewById(R.id.domain);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile_text);
            holder.shangTextView = (TextView) convertView.findViewById(R.id.shang_id);
            holder.zhibaoTextView =  (ImageView) convertView.findViewById(R.id.zhibao_view);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.push_time);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (CarSourceViewHolder) convertView.getTag(R.id.tag_first);
        }
        convertView.setTag(R.id.tag_second, mCarSourceList.get(position));


        if ("review".equals(mCarSourceList.get(position).getStatus())) {
            holder.review.setVisibility(View.VISIBLE);
        } else {
            holder.review.setVisibility(View.GONE);
        }

        if (mCarSourceList.get(position).isIs_certify()) {
            if("dealer".equals(mCarSourceList.get(position).getDealer_category())){
                holder.renzhengImageView.setImageResource(R.drawable.renzheng);
            }else if("4s".equals(mCarSourceList.get(position).getDealer_category())){
                holder.renzhengImageView.setImageResource(R.drawable.renzheng_4s);
            }else{
                holder.renzhengImageView.setImageResource(R.drawable.trans);
            }
            holder.renzhengImageView.setVisibility(View.VISIBLE);
        } else {
            holder.renzhengImageView.setVisibility(View.GONE);
        }

        if (mCarSourceList.get(position).getQs_tags().contains("质保")) {
            holder.zhibaoTextView.setVisibility(View.VISIBLE);
            holder.shangTextView.setVisibility(View.GONE);
        } else {
            holder.zhibaoTextView.setVisibility(View.GONE);
            holder.shangTextView.setVisibility(View.VISIBLE);
            if ("personal".equals(mCarSourceList.get(position).getSource_type())) {
                holder.shangTextView.setText("个");
                holder.shangTextView.setBackgroundResource(R.drawable.sort_ge);
                holder.shangTextView.setTextColor(context.getResources().getColor(R.color.white));
            } else if("dealer".equals(mCarSourceList.get(position).getSource_type()) || "cpo".equals(mCarSourceList.get(position).getSource_type())|| "odealer".equals(mCarSourceList.get(position).getSource_type())){
                holder.shangTextView.setText("商");
                holder.shangTextView.setBackgroundResource(R.drawable.sort_shang);
                holder.shangTextView.setTextColor(context.getResources().getColor(R.color.white));
            }else{
                holder.shangTextView.setVisibility(View.GONE);
            }
        }

        // convertView.setTag();
        holder.brandname.setText(mCarSourceList.get(position).getModel_slug_zh());
        holder.mileTextView.setText(mCarSourceList.get(position).getMile() + "万公里/" + mCarSourceList.get(position).getYear() + "年");
        if ("".equals(mCarSourceList.get(position).getModel_detail_slug_zh())) {
            holder.tvCarTitle.setText(mCarSourceList.get(position).getTitle());
        } else {
            holder.tvCarTitle.setText(mCarSourceList.get(position).getModel_detail_slug_zh());
        }
        holder.tvCarPrice.setText(String.valueOf(mCarSourceList.get(position).getPrice()));

        holder.tvDomain.setText(mCarSourceList.get(position).getDomain());

        String timetitle = Utils.getTimeByTimestamp(mCarSourceList.get(position).getPub_timestamp(), System.currentTimeMillis());
        holder.timeTextView.setText(timetitle);

        String uri = mCarSourceList.get(position).getThumbnail() + "?imageMogr2/thumbnail/!50p";
        Glide.with(context).load(uri).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);


        return convertView;

    }

    public class CarSourceViewHolder {
        public ImageView imgThumbnail;

        public ImageView renzhengImageView;

        public TextView tvCarTitle;

        public TextView timeTextView;

        public ImageView zhibaoTextView;

        public TextView tvCarPrice;

        public TextView review;

        public TextView tvDomain;

        public TextView brandname;

        public TextView shangTextView;

        public TextView mileTextView;
    }
}
